import pyecharts.options as opts
from pyecharts.charts import Bar
from pyecharts.globals import CurrentConfig

from .. import NetworkBuilder

CurrentConfig.ONLINE_HOST = "https://cdn.jsdelivr.net/npm/echarts@latest/dist/"

__all__ = ['plotdgree_distribution']

def plotdgree_distribution(graph):
    """
    绘制度分布图。

    :param graph: 图对象。
    :return:
    """

    # 计算度的分布
    x, p = NetworkBuilder.stat.cal_dgree_distribution(graph)

    # 绘制分布条形图
    b = (
        Bar()
        .add_xaxis(xaxis_data=x)
        .add_yaxis(
            series_name="",
            y_axis=p,
            label_opts=opts.LabelOpts(is_show=False)
        )
        .set_global_opts(
            xaxis_opts=opts.AxisOpts(
                type_="category",
                name="度"
            ),
            yaxis_opts=opts.AxisOpts(
                type_="value",
                name="频率",
            ),
            title_opts=opts.TitleOpts(title="网络度数分布图"),
        )
        .render("./output/degree_distribution.html")
    )
